package tushare

import (
	"tushare/model/mysql"
)

type MoneyFlow struct {
	Id            int64   `json:"id"`
	TsCode        string  `json:"ts_code"`
	TradeDate     string  `json:"trade_date"`
	BuySmVol      float64 `json:"buy_sm_vol"`
	BuySmAmount   float64 `json:"buy_sm_amount"`
	SellSmVol     float64 `json:"sell_sm_vol"`
	SellSmAmount  float64 `json:"sell_sm_amount"`
	BuyMdVol      float64 `json:"buy_md_vol"`
	BuyMdAmount   float64 `json:"buy_md_amount"`
	SellMdVol     float64 `json:"sell_md_vol"`
	SellMdAmount  float64 `json:"sell_md_amount"`
	BuyLgVol      float64 `json:"buy_lg_vol"`
	BuyLgAmount   float64 `json:"buy_lg_amount"`
	SellLgVol     float64 `json:"sell_lg_vol"`
	SellLgAmount  float64 `json:"sell_lg_amount"`
	BuyElgVol     float64 `json:"buy_elg_vol"`
	BuyElgAmount  float64 `json:"buy_elg_amount"`
	SellElgVol    float64 `json:"sell_elg_vol"`
	SellElgAmount float64 `json:"sell_elg_amount"`
	NetMfVol      float64 `json:"net_mf_vol"`
	NetMfAmount   float64 `json:"net_mf_amount"`
	Name          string  `json:"name"`
}

type MoneyFlowCreateStruct struct {
	Id            int64   `json:"id"`
	TsCode        string  `json:"ts_code"`
	TradeDate     string  `json:"trade_date"`
	BuySmVol      float64 `json:"buy_sm_vol"`
	BuySmAmount   float64 `json:"buy_sm_amount"`
	SellSmVol     float64 `json:"sell_sm_vol"`
	SellSmAmount  float64 `json:"sell_sm_amount"`
	BuyMdVol      float64 `json:"buy_md_vol"`
	BuyMdAmount   float64 `json:"buy_md_amount"`
	SellMdVol     float64 `json:"sell_md_vol"`
	SellMdAmount  float64 `json:"sell_md_amount"`
	BuyLgVol      float64 `json:"buy_lg_vol"`
	BuyLgAmount   float64 `json:"buy_lg_amount"`
	SellLgVol     float64 `json:"sell_lg_vol"`
	SellLgAmount  float64 `json:"sell_lg_amount"`
	BuyElgVol     float64 `json:"buy_elg_vol"`
	BuyElgAmount  float64 `json:"buy_elg_amount"`
	SellElgVol    float64 `json:"sell_elg_vol"`
	SellElgAmount float64 `json:"sell_elg_amount"`
	NetMfVol      float64 `json:"net_mf_vol"`
	NetMfAmount   float64 `json:"net_mf_amount"`
}

type ElgVolStruct struct {
	Buy    int64   `json:"buy"`
	Sell   int64   `json:"sell"`
	Jf     int64   `json:"jf"`
	Cf     float64 `json:"cf"`
	BuySm  int64   `json:"buy_sm"`
	SellSm int64   `json:"sell_sm"`
	JfSm   int64   `json:"jf_sm"`
	CfSm   float64 `json:"cf_sm"`
	BuyMd  int64   `json:"buy_md"`
	SellMd int64   `json:"sell_md"`
	JfMd   int64   `json:"jf_md"`
	CfMd   float64 `json:"cf_md"`

	BuyLg  int64   `json:"buy_lg"`
	SellLg int64   `json:"sell_lg"`
	JfLg   int64   `json:"jf_lg"`
	CfLg   float64 `json:"cf_lg"`
}

func MoneyFlowCreate(mf MoneyFlowCreateStruct) {
	mysql.DB.Table("tushare_moneyflow").Create(&mf)
}

func MoneyFlowList(page, pageSize int64, tsCode string) []MoneyFlow {
	offset := (page - 1) * pageSize
	moneyFlowList := []MoneyFlow{}
	mysql.DB.Table("tushare_moneyflow").Select("tushare_moneyflow.id,tushare_moneyflow.ts_code,tushare_moneyflow.trade_date,tushare_moneyflow.net_mf_vol,tushare_moneyflow.net_mf_amount,tushare_stock.name").Joins("left join tushare_stock on tushare_stock.ts_code = tushare_moneyflow.ts_code ").Where("tushare_moneyflow.ts_code = ?", tsCode).Order("tushare_moneyflow.trade_date desc").Limit(pageSize).Offset(offset).Scan(&moneyFlowList)
	return moneyFlowList
}

func MoneyFlowCount(tsCode string) int64 {
	var count int64
	mysql.DB.Table("tushare_moneyflow").Where("tushare_moneyflow.ts_code = ?", tsCode).Count(&count)
	return count
}

func CheckMoneyFlow(tsCode, date string) MoneyFlow {

	moneyFlow := MoneyFlow{}
	mysql.DB.Table("tushare_moneyflow").Where("tushare_moneyflow.ts_code = ? and tushare_moneyflow.trade_date = ?", tsCode, date).Scan(&moneyFlow)
	return moneyFlow
}

//资金流入
func CapitalInflow(date string) []MoneyFlow {
	mfcs := []MoneyFlow{}
	mysql.DB.Table("tushare_moneyflow").Select("tushare_moneyflow.id,tushare_moneyflow.ts_code,tushare_moneyflow.trade_date,tushare_moneyflow.net_mf_vol,tushare_moneyflow.net_mf_amount,tushare_stock.name").Joins("left join tushare_stock on tushare_stock.ts_code = tushare_moneyflow.ts_code ").Where("net_mf_amount > 0 and trade_date = ?", date).Order("net_mf_amount desc").Scan(&mfcs)
	return mfcs
}

func ElgVol(tsCode, tradeDate string, limit int64) ElgVolStruct {
	elgvol := ElgVolStruct{}
	mysql.DB.Table("tushare_moneyflow").Select(`
	SUM(buy_elg_vol) as buy,
SUM(sell_elg_vol) as sell,
SUM(buy_elg_vol) - SUM(sell_elg_vol) as jf,
(SUM(buy_elg_vol) - SUM(sell_elg_vol))/SUM(sell_elg_vol) as cf,

SUM(buy_sm_vol) as buy_sm,
SUM(sell_sm_vol) as sell_sm,
SUM(buy_sm_vol) - SUM(sell_sm_vol) as jf_sm,
(SUM(buy_sm_vol) - SUM(sell_sm_vol))/SUM(sell_sm_vol) as cf_sm,

SUM(buy_md_vol) as buy_md,
SUM(sell_md_vol) as sell_md,
SUM(buy_md_vol) - SUM(sell_md_vol) as jf_md,
(SUM(buy_md_vol) - SUM(sell_md_vol))/SUM(sell_md_vol) as cf_md,

SUM(buy_lg_vol) as buy_lg,
SUM(sell_lg_vol) as sell_lg,
SUM(buy_lg_vol) - SUM(sell_lg_vol) as jf_lg,
(SUM(buy_lg_vol) - SUM(sell_lg_vol))/SUM(sell_lg_vol) as cf_lg

	`).Where("ts_code = ?", tsCode).Where("trade_date <= ?", tradeDate).Limit(limit).Offset(0).Order("trade_date desc").Scan(&elgvol)
	return elgvol
}

func ElgVolByDate(tsCode, tradeDateFrom, tradeDateTo string) ElgVolStruct {
	elgvol := ElgVolStruct{}
	mysql.DB.Table("tushare_moneyflow").Select(`
	SUM(buy_elg_vol) as buy,
SUM(sell_elg_vol) as sell,
SUM(buy_elg_vol) - SUM(sell_elg_vol) as jf,
(SUM(buy_elg_vol) - SUM(sell_elg_vol))/SUM(sell_elg_vol) as cf
	`).Where("ts_code = ?", tsCode).Where("trade_date <= ?", tradeDateFrom).Where("trade_date >= ?", tradeDateTo).Order("trade_date desc").Scan(&elgvol)
	return elgvol
}
